.PHONY: all build test clean install proto bench lint fmt docker help

VERSION ?= v2.0.0
BINARY = onyx-snapshotter
GOFLAGS = -ldflags "-X main.version=$(VERSION) -s -w"
PLATFORMS = linux/amd64 linux/arm64

all: build

build:
	@echo "Building onyx-snapshotter $(VERSION)..."
	go build $(GOFLAGS) -o bin/$(BINARY) .
	@echo "Build complete: bin/$(BINARY)"

build-all:
	@echo "Building for all platforms..."
	@for platform in $(PLATFORMS); do \
		GOOS=$${platform%/*} GOARCH=$${platform#*/} \
		go build $(GOFLAGS) -o bin/$(BINARY)-$${platform%/*}-$${platform#*/} . ; \
	done

test:
	@echo "Running unit tests..."
	go test -v -race -coverprofile=coverage.out ./...
	@echo "Coverage report: coverage.out"

test-dedup:
	@echo "Running deduplication tests..."
	go test -v -race ./test/dedup_test.go

test-integration:
	@echo "Running integration tests..."
	go test -v -tags=integration -timeout=30m ./test/integration/...

bench:
	@echo "Running benchmarks..."
	go test -bench=. -benchmem -run=^$$ ./...

bench-dedup:
	@echo "Benchmarking deduplication..."
	go test -bench=BenchmarkDedup -benchmem ./test/dedup_test.go

coverage:
	@echo "Generating coverage report..."
	go test -coverprofile=coverage.out ./...
	go tool cover -html=coverage.out -o coverage.html
	@echo "Coverage report: coverage.html"

lint:
	@echo "Running linters..."
	golangci-lint run --timeout=5m ./...

fmt:
	@echo "Formatting code..."
	go fmt ./...
	goimports -w .
	gofumpt -l -w .

vet:
	@echo "Running go vet..."
	go vet ./...

clean:
	@echo "Cleaning build artifacts..."
	rm -rf bin/
	rm -f coverage.out coverage.html
	rm -rf /tmp/onyx-test-*

install: build
	@echo "Installing onyx-snapshotter..."
	install -D -m 755 bin/$(BINARY) /usr/local/bin/$(BINARY)
	install -D -m 644 config/config.toml /etc/onyx/config.toml
	install -D -m 644 systemd/onyx-snapshotter.service /etc/systemd/system/onyx-snapshotter.service
	systemctl daemon-reload
	@echo "Installation complete. Start with: sudo systemctl start onyx-snapshotter"

uninstall:
	@echo "Uninstalling onyx-snapshotter..."
	systemctl stop onyx-snapshotter || true
	systemctl disable onyx-snapshotter || true
	rm -f /usr/local/bin/$(BINARY)
	rm -f /etc/systemd/system/onyx-snapshotter.service
	systemctl daemon-reload
	@echo "Uninstallation complete. Data remains in /var/lib/onyx"

docker:
	@echo "Building Docker image..."
	docker build -t onyx-snapshotter:$(VERSION) .
	docker tag onyx-snapshotter:$(VERSION) onyx-snapshotter:latest

docker-push:
	docker push onyx-snapshotter:$(VERSION)
	docker push onyx-snapshotter:latest

release: clean lint test build-all
	@echo "Creating release $(VERSION)..."
	@mkdir -p release
	@for platform in $(PLATFORMS); do \
		tar -czf release/$(BINARY)-$(VERSION)-$${platform%/*}-$${platform#*/}.tar.gz \
			-C bin $(BINARY)-$${platform%/*}-$${platform#*/} \
			-C ../config config.toml \
			-C ../systemd onyx-snapshotter.service ; \
	done
	@echo "Release packages created in release/"

mod:
	@echo "Tidying go modules..."
	go mod tidy
	go mod verify

deps:
	@echo "Downloading dependencies..."
	go mod download

upgrade-deps:
	@echo "Upgrading dependencies..."
	go get -u ./...
	go mod tidy

check: lint vet test
	@echo "All checks passed!"

perf-test:
	@echo "Running performance tests..."
	@echo "Testing cold start time..."
	@scripts/perf-test.sh cold-start
	@echo "Testing dedup ratio..."
	@scripts/perf-test.sh dedup-ratio
	@echo "Testing cache hit rate..."
	@scripts/perf-test.sh cache-hit

stress-test:
	@echo "Running stress tests..."
	go test -v -run=^$$ -bench=. -benchtime=30s -cpu=1,2,4,8 ./...

profile:
	@echo "Running CPU profile..."
	go test -cpuprofile=cpu.prof -bench=. ./...
	@echo "View with: go tool pprof cpu.prof"

memprofile:
	@echo "Running memory profile..."
	go test -memprofile=mem.prof -bench=. ./...
	@echo "View with: go tool pprof mem.prof"

dev-setup:
	@echo "Setting up development environment..."
	go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
	go install golang.org/x/tools/cmd/goimports@latest
	go install mvdan.cc/gofumpt@latest
	@echo "Development tools installed"

.PHONY: help
help:
	@echo "Onyx Snapshotter V2.0 Makefile"
	@echo ""
	@echo "Build Commands:"
	@echo "  make build           Build for current platform"
	@echo "  make build-all       Build for all platforms"
	@echo "  make docker          Build Docker image"
	@echo "  make release         Create release packages"
	@echo ""
	@echo "Test Commands:"
	@echo "  make test            Run unit tests"
	@echo "  make test-dedup      Run dedup tests"
	@echo "  make test-integration Run integration tests"
	@echo "  make bench           Run benchmarks"
	@echo "  make perf-test       Run performance tests"
	@echo "  make stress-test     Run stress tests"
	@echo ""
	@echo "Quality Commands:"
	@echo "  make lint            Run linters"
	@echo "  make fmt             Format code"
	@echo "  make vet             Run go vet"
	@echo "  make check           Run all checks"
	@echo "  make coverage        Generate coverage report"
	@echo ""
	@echo "Install Commands:"
	@echo "  make install         Install to system"
	@echo "  make uninstall       Remove from system"
	@echo ""
	@echo "Development Commands:"
	@echo "  make dev-setup       Setup dev environment"
	@echo "  make deps            Download dependencies"
	@echo "  make mod             Tidy go modules"
	@echo "  make profile         CPU profiling"
	@echo "  make memprofile      Memory profiling"
	@echo ""
	@echo "Maintenance Commands:"
	@echo "  make clean           Clean build artifacts"
	@echo "  make upgrade-deps    Upgrade dependencies"